//
//  SiRollView.swift
//  SiliUtility
//
//  Created by Li Dong on 2023/10/19.
//

import UIKit

class SiRollView: UIView {
    lazy var rollLabel1 = UILabel()
    lazy var rollLabel2 = UILabel()
    
    var dataList = [String]() {
        didSet {
            rollLabel1.text = dataList[min(currentIndex, dataList.count-1)]
            rollLabel2.text = dataList[min(nextIndex, dataList.count-1)]
        }
    }
    var currentIndex: Int = 0
    var nextIndex: Int = 1
    var label1IsTop = true
    var rollEnable: Bool = true

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        rollLabel1.textColor = .init(hex: 0x821113)
        rollLabel1.font = UIFont.Medium(ofSize: 20)
        rollLabel1.textAlignment = .center
        addSubview(rollLabel1)
        rollLabel1.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        rollLabel2.textColor = .init(hex: 0x821113)
        rollLabel2.font = UIFont.Medium(ofSize: 20)
        rollLabel2.textAlignment = .center
        addSubview(rollLabel2)
        rollLabel2.snp.makeConstraints { make in
            make.left.right.height.equalTo(rollLabel1)
            make.top.equalTo(rollLabel1.snp.bottom)
        }
        
        clipsToBounds = true
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    func startRoll() {
        if !rollEnable {
            rollLabel1.isHidden = !label1IsTop
            rollLabel2.isHidden = label1IsTop
            return
        }
//        rollLabel1.text = dataList[min(currentIndex, dataList.count-1)]
//        rollLabel2.text = dataList[min(nextIndex, dataList.count-1)]
        if label1IsTop {
            UIView.animate(withDuration: 0.1) {
                self.rollLabel1.y = -self.height
                self.rollLabel2.y = 0
            } completion: { _ in
                self.rollLabel1.y = self.height
                self.label1IsTop = false
                self.currentIndex += 1
                self.nextIndex += 1
                if self.currentIndex == self.dataList.count {
                    self.currentIndex = 0
                    self.nextIndex = 1
                }
                self.rollLabel1.text = self.dataList[min(self.nextIndex, self.dataList.count-1)]
                self.startRoll()
            }
        }else {
            UIView.animate(withDuration: 0.1) {
                self.rollLabel1.y = 0
                self.rollLabel2.y = -self.height
            } completion: { _ in
                self.rollLabel2.y = self.height
                self.label1IsTop = true
                self.currentIndex += 1
                self.nextIndex += 1
                if self.currentIndex == self.dataList.count {
                    self.currentIndex = 0
                    self.nextIndex = 1
                }
                self.rollLabel2.text = self.dataList[min(self.nextIndex, self.dataList.count-1)]
                self.startRoll()
            }
        }

    }
}
